<?php
/**
 * 留言管理
 * @author cjli
 *
 */   
class GuestbookAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
	} 
	
	//添加留言
	public function add()
	{
	
	}
	 
	public function add_ajax()
	{
		global $user, $language_id;
		$result = array(
			'error' => 1,
			'message' => '',
		);
			$post = $_POST;
			$message_type = intval($post['message_type']);

			if(isset($post['guestbook_token']) && $post['guestbook_token'] != $_SESSION['guestbook_token'])
			{
				$result['message'] = L('_TOKEN_ERROR_');
			}
			elseif( isset($post['title']) && empty($post['title']) ){
					$result['message'] = L('_PLEASE_INPUT_CORRECT_OCCUPATIONAL_INFORMATION_');
			}
			elseif( isset($post['content']) && empty($post['content']) ){
					$result['message'] = L('_PLEASE_INPUT_CORRECT_OCCUPATIONAL_INFORMATION_');
			}
			else{
				$data = array(
					'message_type' => $message_type,
					'product_id' => isset($post['product_id']) ? $post['product_id'] : 0,
					'title' => isset($post['title']) ? trim($post['title']) : '',
					'content' => trim($post['content']),
					'user_id' => isset($user) && $user ? $user['id'] : 0,
					'language_id' => $language_id,
					'username' => isset($post['username']) ? $post['username'] : isset($user) && $user ? $user['first_name']. ' ' . $user['first_name'] : null,
					'email' => isset($post['email']) ? $post['email'] : isset($user) && $user ? $user['email'] : null,
					'data' => $post['data'],
				);
				
				$gbid = CommentModel::insert_guestbook($data);
				if($gbid){
					$result = array(
						'error' => 0,
						'message' => L('_OPERATION_SUCCESS_'),
					);
				}else{
					$result['message'] = L('_OPERATION_FAIL_');
				}
			}
		echo json_encode($result);
	}
	
	//添加预约电话
	public function add_order_phone()
	{
		global $user, $language_id;
		if(!UserModel::logged()){
			redirect(url('user/login'));
		}
		
		if($_POST['dosubmit']){
			$post = $_POST;
			$time_day = explode(' ',$post['yuyue_time']);

			if($time_day['2'] == 'Jan')
				$Month = 1;
			elseif($time_day['2'] == 'Feb')
				$Month = 2;
			elseif($time_day['2'] == 'Mar')
				$Month = 3;
			elseif($time_day['2'] == 'Apr')
				$Month = 4;
			elseif($time_day['2'] == 'May')
				$Month = 5;
			elseif($time_day['2'] == 'Jun')
				$Month = 6;
			elseif($time_day['2'] == 'Jul')
				$Month = 7;
			elseif($time_day['2'] == 'Aug')
				$Month = 8;
			elseif($time_day['2'] == 'Sep')
				$Month = 9;
			elseif($time_day['2'] == 'Oct')
				$Month = 10;
			elseif($time_day['2'] == 'Nov')
				$Month = 11;
			elseif($time_day['2'] == 'Dec')
				$Month = 12;
		
			$time = $time_day['3'].'-'.$Month.'-'.$time_day['1'].' '.$time_day['4'];
			$yuyue_time = strtotime(''.$time.'');
			
			$time2 = $post['day'].' '.$post['hour'].':'.$post['minute'];
			$yuyue_time2 = strtotime(''.$time2.'');
			
			$order_phone_db = D('order_phone');
			$data = array(
				'model_id' => $post['model_id'],
				'language_id' => $language_id,
				'order_sn' => $post['order_sn'],
				'mobile' => $post['mobile'],
				'phone' => $post['phone'],
				'yuyue_time' => $yuyue_time,//北京时间
				'content' => 'null',
				'time' => time(),
				'user_id' => isset($user) && $user ? $user['id'] : 0,
				'username' => isset($user) && $user ? $user['first_name']. ' ' . $user['first_name'] : null,
				'ip' => get_client_ip(),
				'yuyue_time2' => $yuyue_time2,//当地时间
			);
			
			$lastId = $order_phone_db->add($data);
			if($lastId){
				$this->success(L('_OPERATION_SUCCESS_'), url('guestbook/add_order_phone'));
			}else{
				$this->error(L('_OPERATION_FAIL_'), url('guestbook/add_order_phone'));
			}
					
		}else{
		
			$order_db = D('orders');
			$order_item_db = D('orders_items');
			
			$where['orders.user_id'] = $user['id'];
			
			$order_db->field('orders.id,order_sn,order_amount,pay_amount,product_dingjin,order_status,payment_status,shipping_status,start_time, comment.id as is_comment');
			$order_db->order('orders.id DESC');
			$order_db->limit('0,3');
			$order_db->join('`comment` ON comment.order_id = orders.id');
			$order_db->group('orders.id');
			$order_db->where($where);
			$ordersList = $order_db->select();
			if($ordersList){
				foreach ($ordersList as &$row){
					 //订单产品列表
					$productList[$row['id']] = OrderModel::get_order_product_list($row['id']);
				}
			}
	
			$this->assign('ordersList', $ordersList);
			$this->assign('productList', $productList);
			
			//下拉分类
			$select_categorys = CategoryModel::get_category_list(CategoryModel::TYPE_PROBLEM_TYPES); 
			$this->assign('select_categorys', $select_categorys);
			
			$templatefile = THEMES_PATH.'guestbook/add_order_phone.phtml';
			$this->assign('templatefile', $templatefile);
			$this->display('usercenter');	
			//$this->display('add_order_phone');
		}
	}
	
	//我的预约电话列表
	public function my_order_phone()
	{
		global $user, $language_id;
		if(!UserModel::logged()){
			redirect(url('user/login'));
		}
		
		$order_phone_db = D('order_phone');
		
		//分页
		$where['user_id'] = $user['id'];
		$order_phone_db->where($where);
		$count = $order_phone_db->count();//总记录数
		require THEMES_PATH.'page.php';// 导入分页类
		$Page = new Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数
		$pageShow = $Page->show();// 分页显示输出
		$this->assign('pageShow', $pageShow);
		
		
		$order_phone_db->order('id DESC');
		$order_phone_db->limit($Page->firstRow.','.$Page->listRows);
		$order_phone_db->where($where);
		$orderphoneList = $order_phone_db->select();
		$this->assign('orderphoneList', $orderphoneList);
		
		$templatefile = THEMES_PATH.'guestbook/my_order_phone.phtml';
		$this->assign('templatefile', $templatefile);
		$this->display('usercenter');	
			//$this->display('add_order_phone');
	}
	
	//我的预约电话列表
	public function edit_order_phone()
	{
		global $user, $language_id;
		if(!UserModel::logged()){
			redirect(url('user/login'));
		}
		$oid = intval($_GET['id']);
		if($oid){
			$order_phone_db = D('order_phone');
			$orderphone_info = $order_phone_db->where('id='.$oid.' AND user_id='.$user['id'])->find();
			$this->assign('orderphoneInfo', $orderphone_info);
			
			$order_db = D('orders');
			$order_item_db = D('orders_items');
			
			$where['orders.user_id'] = $user['id'];
			
			$order_db->field('orders.id,order_sn,order_amount,pay_amount,product_dingjin,order_status,payment_status,shipping_status,start_time, comment.id as is_comment');
			$order_db->order('orders.id DESC');
			$order_db->limit('0,3');
			$order_db->join('`comment` ON comment.order_id = orders.id');
			$order_db->group('orders.id');
			$order_db->where($where);
			$ordersList = $order_db->select();
			if($ordersList){
				foreach ($ordersList as &$row){
					 //订单产品列表
					$productList[$row['id']] = OrderModel::get_order_product_list($row['id']);
				}
			}
	
			$this->assign('ordersList', $ordersList);
			$this->assign('productList', $productList);
			//下拉分类
			$select_categorys = CategoryModel::get_category_list(CategoryModel::TYPE_PROBLEM_TYPES); 
			$this->assign('select_categorys', $select_categorys);
			$templatefile = THEMES_PATH.'guestbook/edit_order_phone.phtml';
			$this->assign('templatefile', $templatefile);
			$this->display('usercenter');	
			//$this->display('add_order_phone');
		}else{
			$this->error(L('_OPERATION_FAIL_'));
		}
	}
	
	//我的预约电话列表
	public function delete_order_phone()
	{
		global $user, $language_id;
		if(!UserModel::logged()){
			redirect(url('user/login'));
		}
		
		$oid = intval($_GET['id']);
		if($oid){
			$order_phone_db = D('order_phone');
			
			$orderphone_info = $order_phone_db->where('id='.$oid.' AND user_id='.$user['id'])->find();
			
			if($orderphone_info){
				$order_phone_db->delete($oid);
				$this->success(L('_OPERATION_SUCCESS_'), url('guestbook/my_order_phone'));
			}else{
				$this->error(L('_OPERATION_FAIL_'));
			}
		}else{
			$this->error(L('_OPERATION_FAIL_'));
		}
	}
}